package com.jianhua.appmarket.module.service.system.impl;

import com.jianhua.appmarket.common.util.SnowflakeKeyUtil;
import com.jianhua.appmarket.module.dao.system.DictionaryTypeDao;
import com.jianhua.appmarket.module.entity.system.DictionaryType;
import com.jianhua.appmarket.module.model.system.UserVO;
import com.jianhua.appmarket.module.service.system.IDictionaryTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class DictionaryTypeService implements IDictionaryTypeService {

    @Autowired
    private DictionaryTypeDao dictionaryTypeDao;


    @Override
    public Boolean saveOrEditDicType(DictionaryType dictionaryType, UserVO user) {
        if(Objects.isNull(dictionaryType.getId())){
            dictionaryType.setId(SnowflakeKeyUtil.generateId());
            dictionaryType.setCreateUser(user.getNickName());
            dictionaryType.setUpdateUser(user.getNickName());
            return dictionaryTypeDao.insert(dictionaryType) > 0;
        }else {
            return editDicType(dictionaryType,user);
        }
    }

    @Override
    public Boolean editDicType(DictionaryType dictionaryType, UserVO user) {
        dictionaryType.setUpdateUser(user.getNickName());
        return dictionaryTypeDao.update(dictionaryType) > 0;
    }


    @Override
    public int countAllDicTypes() {
        return dictionaryTypeDao.countAll();
    }

    @Override
    public List<DictionaryType> listAllDicTypes() {
        return dictionaryTypeDao.listAll();
    }
}
